package com.parking.mgr.common.config;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.log.Log;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.parking.mgr.common.constant.CommonConstant;
import com.parking.mgr.common.util.IdUtils;
import com.parking.mgr.common.util.TimestampUtil;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.ReflectionException;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName CustomMetaObjectHandler
 * @Description 自定义sql字段填充器，自动填充创建修改相关字段
 * @Auther: zhijianpu
 * @Date: 2023/5/22
 */
@Configuration
public class CustomMetaObjectHandler implements MetaObjectHandler {

    private static final Log log = Log.get();

    private static final String F_INSERT_TIME_MILLIS = "fInsertTimeMillis";

    private static final String F_UPDATE_TIME_MILLIS = "fUpdateTimeMillis";

    private static final String F_ENABLE_STATE = "fEnableState";

    private static final String F_CAN_UPDATE = "fCanUpdate";

    private static final String F_CAN_DELETE = "fCanDelete";

    private static final String F_SORT_NUMBER = "fSortNumber";

    private static final String F_KEY = "fKey";

    private static final String PK_ID = "id";

    @Override
    public void insertFill(MetaObject metaObject) {

        try {
            // id
            setFieldValByName(PK_ID, IdUtils.getId(), metaObject);
            //为空则设置fInsertTimeMillis（BaseEntity)
            if (metaObject.hasGetter(F_INSERT_TIME_MILLIS)) {
                Object fInsertTimeMillis = metaObject.getValue(F_INSERT_TIME_MILLIS);
                if (ObjectUtil.isNull(fInsertTimeMillis)) {
                    long millis = TimestampUtil.createTimestamp();
                    this.strictInsertFill(metaObject, F_INSERT_TIME_MILLIS, Long.class, millis);
                    //setFieldValByName(F_INSERT_TIME_MILLIS, millis, metaObject);
                }
            }

            if (metaObject.hasGetter(F_UPDATE_TIME_MILLIS)) {
                Object fUpdateTimeMillis = metaObject.getValue(F_UPDATE_TIME_MILLIS);
                if (ObjectUtil.isNull(fUpdateTimeMillis)) {
                    long millis = TimestampUtil.createTimestamp();
                    this.strictInsertFill(metaObject, F_UPDATE_TIME_MILLIS, Long.class, millis);
                    //setFieldValByName(F_UPDATE_TIME_MILLIS, millis, metaObject);
                }
            }

            if (metaObject.hasGetter(F_ENABLE_STATE)) {
                //填充是否启用
                Object fEnableState = metaObject.getValue(F_ENABLE_STATE);
                if (ObjectUtil.isEmpty(fEnableState)) {
//                this.strictInsertFill(metaObject, F_ENABLE_STATE, String.class, CommonConstant.ENABLE_STATE);
                    setFieldValByName(F_ENABLE_STATE, CommonConstant.ENABLE_STATE, metaObject);
                }
            }

            if (metaObject.hasGetter(F_CAN_UPDATE)) {
                //填充是否修改
                Object fCanUpdate = metaObject.getValue(F_CAN_UPDATE);
                if (ObjectUtil.isEmpty(fCanUpdate)) {
//                this.strictInsertFill(metaObject, F_CAN_UPDATE, String.class, CommonConstant.CAN_UPDATE);
                    setFieldValByName(F_CAN_UPDATE, CommonConstant.CAN_UPDATE, metaObject);
                }
            }


            if (metaObject.hasGetter(F_CAN_DELETE)) {
                // 填充是否删除
                Object fCanDelete = metaObject.getValue(F_CAN_DELETE);
                if (ObjectUtil.isEmpty(fCanDelete)) {
//                this.strictInsertFill(metaObject, F_CAN_DELETE, String.class, CommonConstant.NOT_CAN_DELETE);
                    setFieldValByName(F_CAN_DELETE, CommonConstant.NOT_CAN_DELETE, metaObject);
                }
            }

            if (metaObject.hasGetter(F_SORT_NUMBER)) {
                // 排序字段
                Object fSortNumber = metaObject.getValue(F_SORT_NUMBER);
                if (ObjectUtil.isEmpty(fSortNumber)) {
                    //this.strictInsertFill(metaObject, F_SORT_NUMBER, Integer.class, CommonConstant.SORT_NUMBER);
                    setFieldValByName(F_SORT_NUMBER, CommonConstant.SORT_NUMBER, metaObject);
                }
            }

            if (metaObject.hasGetter(F_KEY)) {
                // f_key
                Object fKey = metaObject.getValue(F_KEY);
                if (ObjectUtil.isEmpty(fKey)) {
                    //this.strictInsertFill(metaObject, F_KEY, String.class, IdUtils.getKeyUUID());
                    setFieldValByName(F_KEY, IdUtils.getKeyUUID(), metaObject);
                }
            }


        } catch (ReflectionException e) {
            log.warn(">>> CustomMetaObjectHandler处理过程中无相关字段，不做处理");
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        try {
            long millis = TimestampUtil.createTimestamp();
            // 设置更新时间
            this.strictUpdateFill(metaObject, F_UPDATE_TIME_MILLIS, Long.class, millis);
//            setFieldValByName(F_UPDATE_TIME_MILLIS, millis, metaObject);
        } catch (ReflectionException e) {
            log.warn(">>> CustomMetaObjectHandler处理过程中无相关字段，不做处理");
        }
    }
}
